package com.junzhiit.nengji.app.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.app.service.OrdersGoodsService;
import com.junzhiit.nengji.app.web.exception.GoodsNotFoundException;
import com.junzhiit.nengji.app.web.exception.OrdersGoodsNotFoundException;
import com.junzhiit.nengji.app.web.exception.RefundMoneyFailException;
import com.junzhiit.nengji.app.web.util.tools.NumberUtils;
import com.junzhiit.nengji.app.web.util.wechat.WeChatUtils;
import com.junzhiit.nengji.app.web.util.wechat.bean.RefundOrder;
import com.junzhiit.nengji.app.web.util.wechat.bean.RefundResult;
import com.junzhiit.nengji.app.web.vo.OrdersGoodsVO;
import com.junzhiit.nengji.app.web.vo.OrdersRefundVO;
import com.junzhiit.nengji.app.web.vo.ResultVO;
import com.junzhiit.nengji.bo.OrdersGoodsBO;
import com.junzhiit.nengji.dao.GoodsDAO;
import com.junzhiit.nengji.dao.OrdersDAO;
import com.junzhiit.nengji.dao.OrdersGoodsDAO;
import com.junzhiit.nengji.dao.OrdersRefundDAO;
import com.junzhiit.nengji.model.Goods;
import com.junzhiit.nengji.model.Orders;
import com.junzhiit.nengji.model.OrdersGoods;
import com.junzhiit.nengji.model.OrdersRefund;
import com.junzhiit.nengji.model.enums.ApplyRefundIdentity;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.OrderStatus;
import com.junzhiit.nengji.model.enums.OrdersRefundStatus;

@Service
public class OrdersGoodsServiceImpl extends BaseServiceImpl implements OrdersGoodsService {

	@Autowired
	private OrdersGoodsDAO ordersGoodsDAO;

	@Autowired
	private GoodsDAO goodsDAO;
	
	@Autowired
	private OrdersDAO ordersDAO;
	
	@Autowired
	private OrdersRefundDAO ordersRefundDAO;
	
	@Override
	public ResultVO findAll(String orderId) {
		ResultVO result = new ResultVO();
		List<OrdersGoodsBO> list = ordersGoodsDAO.getOrderGoodsOrderId(orderId);
		List<OrdersGoodsVO> voList = new ArrayList<OrdersGoodsVO>();
		for(OrdersGoodsBO bo :list){
			OrdersGoodsVO vo = new OrdersGoodsVO();
			BeanUtils.copyProperties(bo,vo);
			voList.add(vo);
		}
		result.setData(voList);
		return result;
	}

	@Override
	public ResultVO getOrderCountByGoodsId(String goodsId) {
		ResultVO result = new ResultVO();
		Goods goods = goodsDAO.query(goodsId);
		if (goods == null) {
			throw new GoodsNotFoundException("商品不存在");
		}
		int data = ordersGoodsDAO.getOrderCountByGoodsId(goodsId);
		result.setData(data);
		return result;

	}

	@Override
	public ResultVO update(OrdersRefundVO ordersRefundVO) {
		ResultVO result = new ResultVO();
		//更新订单商品状态
		boolean flag = true;
		OrdersGoods ordersGoods = ordersGoodsDAO.query(ordersRefundVO.getOrdersGoodsId());
		if(ordersGoods == null){
			throw new OrdersGoodsNotFoundException("订单商品不存在");
		}
		if(goodsDAO.query(ordersGoods.getGoodsId())==null){
			throw new GoodsNotFoundException("商品不存在");
		}
		ordersGoods.setIsRefundMoney(Is.YES.toIntValue());
		ordersGoodsDAO.update(ordersGoods);
		//更新订单状态
		List<OrdersGoodsBO> list = ordersGoodsDAO.getOrderGoodsOrderId(ordersRefundVO.getOrdersId());
		Orders orders = ordersDAO.query(ordersRefundVO.getOrdersId());
		for(int i=0;i<list.size();i++){
			if(list.get(i).getIsRefundMoney() == Is.NO.toIntValue()){
				flag=false;
				break;
			}
		}
		if(flag){
			orders.setStatus(OrderStatus.FAILIN.toIntValue());
			ordersDAO.update(orders);
		}
		//插入退款记录
		OrdersRefund ordersRefund = new OrdersRefund();
		OrdersGoodsBO goods = ordersGoodsDAO.getOrdersGoods(ordersRefundVO.getOrdersGoodsId());
		BeanUtils.copyProperties(ordersRefundVO, ordersRefund);
		if(goods.getIsRefundGoods() == Is.YES.toIntValue()){
			ordersRefund.setGoodsRefundId(goods.getGoodsRefundId());
		}
		ordersRefund.setShopId(goods.getShopId());
		ordersRefund.setStatus(OrdersRefundStatus.SHOP_AGREE.toIntStatus());
		ordersRefund.setType(ApplyRefundIdentity.SHOP.toIntValue());
		ordersRefund.setShopDealTime(new Date());
		ordersRefund.setRefundNumber(NumberUtils.getMchBillno());
		ordersRefundDAO.insert(ordersRefund);
		return result;
	}

	@Override
	public ResultVO findInfo(String ordersGoodsId) {
		ResultVO result = new ResultVO();
		OrdersGoods ordersGoods=ordersGoodsDAO.query(ordersGoodsId);
		Double price = ordersGoods.getPrice();
		result.setData(price);
		return result;
	}

	@Override
	public ResultVO applyRefund(OrdersRefundVO ordersRefundVO) {
		ResultVO result = new ResultVO();
		boolean flag = true;
		Orders orders = ordersDAO.query(ordersRefundVO.getOrdersId());
		//退款
		RefundOrder refundOrder = new RefundOrder();
		refundOrder.setTransactionId(orders.getTransactionNumber());
		refundOrder.setOutRefundNo(NumberUtils.getMchBillno());
		refundOrder.setTotalFee(Math.round(orders.getPayMoney()*100));
		refundOrder.setRefundFee(Math.round(ordersRefundVO.getMoney()*100));
		refundOrder.setRefundDesc(ordersRefundVO.getReason());
		RefundResult refundResult = WeChatUtils.refund(refundOrder);
		if(refundResult == null){
			throw new RefundMoneyFailException("退款失败！");
		}
		
		//更新订单商品状态
		OrdersGoods ordersGoods = ordersGoodsDAO.query(ordersRefundVO.getOrdersGoodsId());
		if(ordersGoods == null){
			throw new OrdersGoodsNotFoundException("订单商品不存在");
		}
		if(goodsDAO.query(ordersGoods.getGoodsId())==null){
			throw new GoodsNotFoundException("商品不存在");
		}
		ordersGoods.setIsRefundMoney(Is.NO.toIntValue());
		ordersGoodsDAO.update(ordersGoods);
		//更新订单状态
		List<OrdersGoodsBO> list = ordersGoodsDAO.getOrderGoodsOrderId(ordersRefundVO.getOrdersId());
		for(int i=0;i<list.size();i++){
			if(list.get(i).getIsRefundMoney() == Is.NO.toIntValue()){
				flag=false;
				break;
			}
		}
		if(flag){
			orders.setStatus(OrderStatus.FAIL.toIntValue());
			ordersDAO.update(orders);
		}
		
		//插入退款表数据
		OrdersRefund ordersRefund = new OrdersRefund();
		OrdersGoodsBO goods = ordersGoodsDAO.getOrdersGoods(ordersRefundVO.getOrdersGoodsId());
		BeanUtils.copyProperties(ordersRefundVO, ordersRefund);
		if(goods.getIsRefundGoods() == Is.YES.toIntValue()){
			ordersRefund.setGoodsRefundId(goods.getGoodsRefundId());
		}
		ordersRefund.setShopId(goods.getShopId());
		ordersRefund.setStatus(OrdersRefundStatus.COMPLETE.toIntStatus());
		ordersRefund.setType(ApplyRefundIdentity.PLATFORM.toIntValue());
		ordersRefund.setPlatformTime(new Date());
		ordersRefund.setCompleteTime(new Date());
		ordersRefund.setRefundNumber(refundResult.getOutRefundNo());
		ordersRefund.setWechatRefundNumber(refundResult.getRefundId());
		ordersRefundDAO.insert(ordersRefund);
		
		return result;
	}

	@Override
	public void updateCanRefund() {
		Date date = DateUtil.addDays(new Date(), -7);
		List<OrdersGoods> ordersGoods = ordersGoodsDAO.getCanRefundGoods(date);
		for(OrdersGoods goods : ordersGoods){
			goods.setCanRefund(Is.NO.toIntValue());
			ordersGoodsDAO.update(goods);
		}
	}
}
